package com.aoasaa;


import com.userrequestdtos.RetrivingNotesDTO;
import com.userrequestdtos.ViewingCategoryAndTitleDTO;
import com.userrequestdtos.ViewingTitlesDTO;
import com.userutils.WSConnectionFile;
import com.userutils.WSConnectionStrings;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; 
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class UserCustomSearching extends Activity implements OnItemSelectedListener 
{
	public SharedPreferences sp_obj;
	public Editor editor_obj;
	 Spinner sp_list;    
	 AutoCompleteTextView auto_category,auto_tags;
	 static String[] categorylist={"h","k"}, titlelist={"h","k"}, retrivningnoteslist={"h","k"};
	@Override    
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customsearchfile);  
        
     // accessing shared preference     
     		sp_obj=getSharedPreferences("ArchiveApp",MODE_WORLD_WRITEABLE);
     		editor_obj=sp_obj.edit();
     		
        auto_category=(AutoCompleteTextView)findViewById(R.id.autoText_category);

        auto_tags=(AutoCompleteTextView)findViewById(R.id.autoText_tag);
        auto_tags.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v)  
			{ 
				// logic on click of this component
				ViewingTitlesDTO obj_viewtitle=new ViewingTitlesDTO("sharan", auto_category.getEditableText().toString());
				WSConnectionStrings.parameterobjectvalue=obj_viewtitle;
				WSConnectionStrings.objectname="obj_viewtitle"; 
				WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewTitles;
				new MyAsyncTask123().execute();
			}
		}); 
        
        // get info button logic
        
        Button btn_getinfo=(Button)findViewById(R.id.btn_customsearch_getinfo);
        btn_getinfo.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
 
				AutoCompleteTextView auto_category=(AutoCompleteTextView)findViewById(R.id.autoText_category);
				Toast.makeText(UserCustomSearching.this, auto_category.getEditableText().toString() + ""+ auto_tags.getEditableText().toString(), Toast.LENGTH_LONG).show();
				RetrivingNotesDTO obj_retrivenotes=new RetrivingNotesDTO("sharan", auto_category.getEditableText().toString(),auto_tags.getEditableText().toString());
				WSConnectionStrings.parameterobjectvalue=obj_retrivenotes;
				WSConnectionStrings.objectname="obj_retrivenotes"; 
				WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.METHOD_retrivingNotes;
				new MyAsyncTask123().execute(); 
			}
		});
             
        String notestype[]={"Text Notes","Image Notes","Voice Notes"};
        // creating references for components of custom search screen
        // spinner
        
       sp_list=(Spinner)findViewById(R.id.spinner_customsearchlist);
       sp_list.setAdapter(new ArrayAdapter(UserCustomSearching.this, android.R.layout.simple_list_item_1,notestype));
       sp_list.setOnItemSelectedListener(this);
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View v, int position,
			long arg3) {
		String filetype="";
		if(position==0)
		{
		    filetype="text";	
		}
	    else if(position==1)
		{
	    	filetype="image";
		}
		else if(position==2)    
		{
			filetype="voice";
		}
		editor_obj.putString("retrivednote_type", filetype);
		editor_obj.commit(); 
		Toast.makeText(v.getContext(), ""+sp_list.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
		// sending request to server to fetch the selected notes categories
		// fetching user categories 
				ViewingCategoryAndTitleDTO obj_viewcategorytitle =new ViewingCategoryAndTitleDTO("sharan",filetype);
				// assigning the object values, object names and method name
				WSConnectionStrings.parameterobjectvalue=obj_viewcategorytitle;
				WSConnectionStrings.objectname="obj_viewcategorytitle"; 
				WSConnectionStrings.USER_METHOD_NAME=WSConnectionStrings.Method_viewCategories;
				new MyAsyncTask123().execute();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	// inner class  For Network Connection i.e, for Web service connection
		class MyAsyncTask123 
		extends AsyncTask<Void, String, String>
		{
			@Override
			protected void onPostExecute(String response) 
			{
	              // If response comes while viewing category
				if(WSConnectionStrings.objectname.equals("obj_viewcategorytitle")) 
				{ 
					categorylist=UserXMLParsingFile.responseOperation(UserCustomSearching.this,response, "", "categoryname");
					Log.i("categorylist", ""+categorylist);
                    auto_category.setAdapter(new ArrayAdapter(UserCustomSearching.this, android.R.layout.simple_list_item_1,categorylist));
					Log.i("categorylistcheck", ""+categorylist); 
				}      
				else if(WSConnectionStrings.objectname.equals("obj_viewtitle"))
				{
					titlelist=UserXMLParsingFile.responseOperation(UserCustomSearching.this,response, "", "titlename");
					Log.i("titlelist", ""+titlelist);
                    auto_tags.setAdapter(new ArrayAdapter(UserCustomSearching.this, android.R.layout.simple_list_item_1,titlelist));
					Log.i("titlelistcheck", ""+titlelist); 
				}    
				else if(WSConnectionStrings.objectname.equals("obj_retrivenotes"))
				{
					retrivningnoteslist=UserXMLParsingFile.responseOperation(UserCustomSearching.this,response, "filepath", "content");
					Log.i("retrivningnoteslist", ""+retrivningnoteslist);
                   // auto_tags.setAdapter(new ArrayAdapter(UserCustomSearching.this, android.R.layout.simple_list_item_1,titlelist));
					Log.i("retrivningnoteslistCheck", ""+retrivningnoteslist); 
				   editor_obj.putString("retrivedContent", retrivningnoteslist[0]);
				   editor_obj.putString("retrivedurl", retrivningnoteslist[1]);
				   editor_obj.commit();            
				   
				   // navigating to viewing Retrieved notes
				        
					Intent i=new Intent(UserCustomSearching.this,ViewingAllNotes.class);
		            startActivity(i);
				}   
			}  
			@Override 
			protected String doInBackground(Void... params) {
				// your network operation
				String wsresponse=WSConnectionFile.wsConn(WSConnectionStrings.parameterobjectvalue, WSConnectionStrings.objectname, 
						WSConnectionStrings.USER_METHOD_NAME,
						WSConnectionStrings.NAMESPACE+WSConnectionStrings.USER_METHOD_NAME);
				return wsresponse; 
			}        
		}
}

